﻿using System;
using System.ComponentModel;
using System.Linq;
using StyleMVVM.ViewModel;
using Wala.Connection.Objects.Authen;
using Wala.Connection.Managements;
using Windows.UI.Popups;
using Wala.Connection.Objects.Profiles;
using Wala.Connection.Helpers;
using Wala.Connection.ComConnections;

namespace Wala.ViewModels.Authen
{
    public class LoginViewModel : PageViewModel, INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;

        #region //---------- Private Fields ----------//

        private string phoneNumber;
        private string password;

        private DelegateCommand loginCommand;
        #endregion

        #region //---------- CLR Properties ---------//

        public DelegateCommand LoginCommand
        {
            get
            {
                if (loginCommand == null)
                {
                    loginCommand = new DelegateCommand(LoginExcute);
                }

                return loginCommand;
            }            
        }

        public string PhoneNumber
        {
            get { return phoneNumber; }
            set 
            {
                if (phoneNumber != value)
                {
                    phoneNumber = value;
                    OnPropertyChanged("PhoneNumber");
                }
            }
        }

        public string Password
        {
            get { return password; }
            set 
            {
                if (password != value)
                {
                    password = value;
                    OnPropertyChanged("Password");
                }
            }
        }

        #endregion

        #region //---------- Constructors ----------//

        public LoginViewModel()
        {
            this.phoneNumber = "84972263179";
            this.password = "chuonggio1";

            ConnectionManagement conn = ConnectionManagement.Instance;
            conn.Start();
            conn.MainViewMessageTransfered += new MessageTransferedEventHanler(ShowMessage);
        }
        
        #endregion

        #region //----------- Private Methods ---------//

        private async void LoginExcute(object parameter)
        {
            if ((string.Compare(phoneNumber.Trim(), "") == 0) || (string.Compare(password.Trim(), "") == 0))
            {
                MessageDialog mess = new MessageDialog("Phone number/password has not been entered. Please enter your details correctly and try logging in again.");
                mess.ShowAsync();
            }
            else
            {
                bool success = await Client.AuthenticateAsync(this.PhoneNumber, this.Password);
                if (!success)
                {
                    MessageDialog mess = new MessageDialog("Phone number/password you entered is incorrect.");
                    mess.ShowAsync();
                }
                else
                {
                    var objectStorageHelper = new StorageHelper<RetAuthen>(StorageType.Local);
                    RetAuthen retAuthen = await objectStorageHelper.LoadASync("RetAuthen");

                    ShortProfile curProfile = await ProfileManager.GetProfile(retAuthen.User.Id);
                    var objectStorageHelperProfile = new StorageHelper<ShortProfile>(StorageType.Local);
                    objectStorageHelperProfile.SaveASync(curProfile, "CurrentUser");
                    Navigation.Navigate("MainPage",curProfile);
                }
            }
        }

        private void ShowMessage(object sender, Connection.Argument.NotifyEventArgs notifyEventArgs)
        {
            MessageDialog m = new MessageDialog("a");
            m.ShowAsync();
        }


        #endregion

        #region //----------- Protected Methods ----------//

        protected virtual void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion
    }
}
